﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ShitLibrary
{
    /// <summary>
    /// 资源缓存.
    /// </summary>
    public class SharedResourceDictionary : ResourceDictionary
  {
    /// <summary>
    /// Internal cache of loaded dictionaries 
    /// </summary>
    public static Dictionary<Uri, ResourceDictionary> m_sharedDictionaries = new Dictionary<Uri, ResourceDictionary>();

    /// <summary>
    /// Returns or sets the uniform resource identifier (URI) to load resources from.
    /// </summary>
    public new Uri Source
    {
      get { return _sourceUri; }
      set
      {
        _sourceUri = value;

        if (m_sharedDictionaries.ContainsKey(value))
        {
          // If the dictionary is already loaded, get it from the cache
          MergedDictionaries.Add(m_sharedDictionaries[value]);
        }
        else
        {
          // If the dictionary is not yet loaded, load it by setting
          // the source of the base class
          base.Source = value;

          // add it to the cache
          m_sharedDictionaries.Add(value, this);
        }
      }
    }

    /// <summary>
    /// 本地资源解析工具
    /// </summary>
    private Uri _sourceUri;
  }

  public static class SharedDictionaryManager
  {
    public static string ThemesDirectory
    {
      get
      {
        var location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        return Path.Combine(location, @"CControlTemplates\");
      }
    }

    #region Uri
    public static Uri UMLTreeViewDictionaryUri
    {
      get { return new Uri(Path.Combine(ThemesDirectory, "UMLTreeViewTemplate.xaml")); }
    }
    #endregion

    #region Resource Dictionary
    public static ResourceDictionary UMLTreeViewDictionary
    {
      get
      {
        if (_tmlTreeDictoray is null)
        {
          _tmlTreeDictoray = new ResourceDictionary() { Source = UMLTreeViewDictionaryUri };
        }

        return _tmlTreeDictoray;
      }
    }

     
    #endregion

    #region Private fields
    private static ResourceDictionary _tmlTreeDictoray;
    #endregion
  }
}
